package com.kady.filter;


import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * \* Created with IntelliJ IDEA.
 * \* User: kady
 * \* Date: 2021/3/23
 * \* Time: 17:15
 * \* 千峰JAVA2101班
 * \* Description: 权限拦截控制
 * \
 */

@WebFilter("/front/*")
public class FrontAuthFilter implements Filter {
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest httpServletRequest =  (HttpServletRequest) servletRequest;
        HttpServletResponse httpServletResponse =  (HttpServletResponse) servletResponse;
        Object obj = httpServletRequest.getSession().getAttribute("frontUser");

        String uri = httpServletRequest.getRequestURI();
        if(!uri.endsWith("html") && !uri.endsWith("css") && !uri.endsWith("js")) {
//            if(obj != null){
            if(true){
                filterChain.doFilter(servletRequest,servletResponse);
            }else{
                System.out.println("url拦截.......");
                httpServletResponse.sendRedirect(httpServletRequest.getContextPath()+"/login.jsp");
            }
        }else{
            filterChain.doFilter(servletRequest,servletResponse);
        }

    }

    @Override
    public void destroy() {

    }
}
